package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Bank;
import com.csjy.migration.hnnd.dict.RepayType;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.*;


@BusinessDataConverter(
    auto = false,

    insertSql = "INSERT INTO ND_GUARANTEE_DETAILS\n" +
        "(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, INSURANCE_BANK, GUARANTEE_CONTRACT_NO, LOAN_CONTRACT_NUMBER, CONTRACT_START_TIME, CONTRACT_END_TIME, PRO_ID, GUARANTEED_CONTRACT_AMOUNT, APPLICABLE_LOAN_AMOUNT, AUDIT_PASS_SIGN, PAID_AMOUNT, PROC_ID, INVALID_SIGN, BANK_BRANCH_NAME, IS_VALID, ANNUAL_INTEREST, REPAYMENT_METHODMETHOD, BORROWING_START_TIME, BORROWING_END_TIME, VIRTUAL_VOID_MARK, OBSOLETE_REVIEW_MARK, LOAN_NOTICE_STATUS, CONTRACT_TYPE, CONTRACT_TERM, REPAYMENT_METHOD, REPALY_MONEY, SIGNEDORNOT)\n" +
        "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",

    updateSql = "UPDATE ND_GUARANTEE_DETAILS\n" +
        "SET CREATE_NAME=?, CREATE_BY=?, CREATE_DATE=?, UPDATE_NAME=?, UPDATE_BY=?, UPDATE_DATE=?, SYS_ORG_CODE=?, SYS_COMPANY_CODE=?, BPM_STATUS=?, INSURANCE_BANK=?, GUARANTEE_CONTRACT_NO=?, LOAN_CONTRACT_NUMBER=?, CONTRACT_START_TIME=?, CONTRACT_END_TIME=?, PRO_ID=?, GUARANTEED_CONTRACT_AMOUNT=?, APPLICABLE_LOAN_AMOUNT=?, AUDIT_PASS_SIGN=?, PAID_AMOUNT=?, PROC_ID=?, INVALID_SIGN=?, BANK_BRANCH_NAME=?, IS_VALID=?, ANNUAL_INTEREST=?, REPAYMENT_METHODMETHOD=?, BORROWING_START_TIME=?, BORROWING_END_TIME=?, VIRTUAL_VOID_MARK=?, OBSOLETE_REVIEW_MARK=?, LOAN_NOTICE_STATUS=?, CONTRACT_TYPE=?, CONTRACT_TERM=?, REPAYMENT_METHOD=?, REPALY_MONEY=?, SIGNEDORNOT=?\n" +
        "WHERE ID = ?",

    findMigratedIdListSql = "SELECT ID FROM ND_GUARANTEE_DETAILS WHERE PRO_ID = ?"
)
public class NdGuaranteeDetails extends AbstractBusiness {
  private Set<String> ids = new HashSet<String>();

  public NdGuaranteeDetails(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
  }

  @Override
  public void findMigratedIdList() throws SQLException {
    this.migratedIdList = this.targetDbUtil.selectSingleColumn(this.findMigratedIdListSql, Arrays.asList(new String[]{this.projectId}));
  }

  public void migrate() {

  }

  public void customMigrate(Map row) throws Exception {

    if(this.ids.contains(row.get("sourceId").toString())){
      return;
    }

    int needMigratedResult = this.needMigrated(row.get("sourceId").toString());

    if(needMigratedResult == 0) {
      return;
    }
    if(needMigratedResult == 1) {
      this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);
//      this.batchUpdateValues.add(this.buildValues(row));
    } else {
      this.targetDbUtil.updateByParams(insertSql,this.buildValues(row),false);
//      this.batchInsertValues.add(this.buildValues(row));
    }

    ids.add(row.get("sourceId").toString());
  }

  public List buildValues(Map row) throws Exception {
    List values = new ArrayList();
    Long contractTerm = BusinessTool.calcMonthBetweenTwoDate(row.get("promContBtime"),row.get("promContEtime"));
    // ID
    values.add(row.get("sourceId").toString());
    // CREATE_NAME
    values.add(null);
    // CREATE_BY
    values.add(null);
    // CREATE_DATE
    values.add(null);
    // UPDATE_NAME
    values.add(null);
    // UPDATE_BY
    values.add(null);
    // UPDATE_DATE
    values.add(null);
    // SYS_ORG_CODE
    values.add(null);
    // SYS_COMPANY_CODE
    values.add("002");
    // BPM_STATUS
    values.add(1);
    //INSURANCE_BANK 出保银行名称 11 VARCHAR2(50) [NULL] false [NULL]
    values.add(DictTool.getTargetDictCode(Bank.class,row.get("promBank")));
    //GUARANTEE_CONTRACT_NO 保证合同编号 12 VARCHAR2(128) [NULL] false [NULL]
    values.add(row.get("promContractNo"));
    //LOAN_CONTRACT_NUMBER 借款合同编号 13 VARCHAR2(128) [NULL] false [NULL]
    values.add(row.get("loanContractNo"));
    //CONTRACT_START_TIME 保证合同开始时间 14 DATE [NULL] false [NULL]
    values.add(Tool.toDate(row.get("promContBtime")));
    //CONTRACT_END_TIME 保证合同到期时间 15 DATE [NULL] false [NULL]
    values.add(Tool.toDate(row.get("promContEtime")));
    //PRO_ID 项目ID 16 VARCHAR2(36) [NULL] false [NULL]
    values.add(this.projectId);
    //GUARANTEED_CONTRACT_AMOUNT 签订保证合同金额 17 NUMBER(20,6) [NULL] false [NULL]
    values.add(row.get("guaranteedContractAmount"));
    //APPLICABLE_LOAN_AMOUNT 可用放款余额 18 NUMBER(20,6) [NULL] false [NULL] todo
    if(row.get("guaranteedContractAmount") == null) {
      values.add(null);
    } else if(row.get("paidAmount") == null){
      values.add(row.get("guaranteedContractAmount"));
    } else {
      values.add(((BigDecimal)row.get("guaranteedContractAmount")).subtract((BigDecimal)row.get("paidAmount")));
    }

    //AUDIT_PASS_SIGN 审核通过标志 19 VARCHAR2(32) [NULL] false [NULL]
    values.add("1");
    //PAID_AMOUNT 已放款金额 20 NUMBER(20,6) [NULL] false [NULL]
    values.add(Tool.covertToBigDecimal(row.get("paidAmount")));
    //PROC_ID 流程ID 21 VARCHAR2(36) [NULL] false [NULL]
    values.add(row.get("procInstId")+ "-JW");
    //INVALID_SIGN 作废标志 22 VARCHAR2(32) [NULL] false [NULL]
    values.add("1");
    //BANK_BRANCH_NAME 银行支行名称 23 VARCHAR2(100) [NULL] false [NULL]
    values.add(row.get("bankBranchName"));
    //IS_VALID 是否使用 24 VARCHAR2(32) [NULL] false [NULL]
    values.add("Y");
    //ANNUAL_INTEREST 年利率 28 NUMBER(20,6) [NULL] false [NULL]
    values.add(Tool.covertToBigDecimal(row.get("yearRate")));
    //REPAYMENT_METHODMETHOD 客户还款方式 29 VARCHAR2(50) [NULL] false [NULL]
    values.add(sourceData.getRepayType());
    //BORROWING_START_TIME 借款开始时间 30 DATE [NULL] false [NULL]
    values.add(Tool.toDate(row.get("promContBtime")));
    //BORROWING_END_TIME 借款结束时间 31 DATE [NULL] false [NULL]
    values.add(Tool.toDate(row.get("promContEtime")));
    // VIRTUAL_VOID_MARK 虚拟状态
    values.add("1");
    // OBSOLETE_REVIEW_MARK 作废状态审核表及
    values.add("");
    // LOAN_NOTICE_STATUS 放款通知书状态
    values.add("1");
    //CONTRACT_TYPE 合同类型 32 VARCHAR2(32) [NULL] false [NULL] 保证-1
    values.add(row.get("contractType"));
    //CONTRACT_TERM 合同期限 33 VARCHAR2(32) [NULL] false [NULL]
    values.add(contractTerm);
    // TODO: REPAYMENT_METHOD  34 VARCHAR2(4000) [NULL] false [NULL] 字典
    values.add(row.get("repaymentMethod"));
    //REPALY_MONEY 已还款金额 35 NUMBER(20,6) [NULL] false [NULL]
    values.add(Tool.covertToBigDecimal(row.get("repalyMoney")));
    //SIGNEDORNOT 是否签章 36 VARCHAR2(128) [NULL] false [NULL]
    values.add(null);
    return values;
  }
}
